package com.myk.game.sumeru.game;

import com.myk.game.sumeru.interfaces.IGame;
import com.myk.game.sumeru.interfaces.IGameButtonModel;
import com.myk.game.sumeru.interfaces.IGameUI;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseGame implements IGame {

    protected IGameUI ui;
    protected Map map;
    protected List<Chess> chesses;

    @Override
    public void init(IGameUI gameUI) {
        ui = gameUI;
        map = new Map();
        chesses = new ArrayList<>();
        ui.initChessBoard();
        ui.setGameButtons(
                makeGameButton("返回", ui::leave),
                makeGameButton("开始", this::start)
        );
        ui.showTip("请点击开始游戏");
    }

    /**
     * 设置自定义游戏按钮
     */
    public IGameButtonModel makeGameButton(String text, Runnable event) {
        return new IGameButtonModel() {
            @Override
            public String getButtonText() {
                return text;
            }

            @Override
            public Runnable getButtonEvent() {
                return event;
            }
        };
    }

    @Override
    public void start() {

    }

    @Override
    public Map getMap() {
        return map;
    }

    @Override
    public List<Chess> getChesses(int party) {
        List<Chess> cs = new ArrayList<>();
        for (Chess c : chesses) {
            if (c.getParty() == party) {
                cs.add(c);
            }
        }
        return cs;
    }

    @Override
    public IGameUI getGameUi() {
        return ui;
    }

}
